import akshare as ak
from connector import mysqlConnectorHelper as mc
import datetime
import SimpleMySqlClass as smc


def get_all_stock():
    ## A 股上市公司的实时行情数据
    stock_zh_a_spot_df = ak.stock_zh_a_spot()
    print(stock_zh_a_spot_df)
    return stock_zh_a_spot_df


def get_jd_info(code):
    stock_data_df = ak.stock_financial_analysis_indicator(code, "2023")  # 财务指标数据
    # print(df.head())
    stock_data_df = stock_data_df.set_index(stock_data_df["日期"])
    print(stock_data_df)

    for index, row in stock_data_df.iterrows():
        jsy = row["扣除非经常性损益后的净利润(元)"]
        jsy = row["每股经营性现金流(元)"]
        jsy = row["资产负债率(%)"]
        jsy = row["净资产收益率(%)"]
        rq = row["日期"]
        print(rq, " 净资产收益率(%) = ", jsy)

    return stock_data_df


def get_pe_pb_ps(code):
    stock_data_df = ak.stock_a_indicator_lg(code)
    print(stock_data_df)
    stock_data_df = stock_data_df.loc[:, ["trade_date", "pe", "pb", "ps"]]
    # start_date = datetime.datetime.now() - datetime.timedelta(days=90)
    # dateStart = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)##过去30天的数据
    # dateStart = datetime.datetime.strptime(str(dateStart),'%Y-%m-%d %H:%M:%S')
    # dateStart = datetime.datetime.date(dateStart)
    # stock_data_df = ak.stock_a_lg_indicator("601398")
    # df2_mean = stock_data_df[stock_data_df.trade_date > dateStart].pe.mean()
    # var2 = df2_mean > 0 and df2_mean < 30
    stock_data_df["date_time"] = stock_data_df.loc[:, ["trade_date"]]
    stock_data_df["code"] = code
    # start_date = datetime.datetime.now() - datetime.timedelta(days=30)
    start_date = datetime.datetime.date(
        datetime.datetime.now() - datetime.timedelta(days=90)
    )
    start_date = datetime.datetime.strptime("2024-01-01", "%Y-%m-%d")
    stock_data_df2 = stock_data_df[
        stock_data_df.trade_date > datetime.datetime.date(start_date)
    ]
    print(stock_data_df2)
    return stock_data_df


if __name__ == "__main__":
    code = "002202"
    get_pe_pb_ps(code)
    print("main")
